//
//  YQOrderDetailsBottonView.m
//  MuYing
//
//  Created by tbb on 2019/8/1.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "YQOrderDetailsBottonView.h"
#import "YQGoodsOrderDetailsModel.h"

#define kBTNTAG_BASE 11100

@interface YQOrderDetailsBottonView ()

@end

@implementation YQOrderDetailsBottonView

-(void)setNewsOrderModel:(YQGoodsOrderDetailsModel *)newsOrderModel {
    _newsOrderModel = newsOrderModel;
    self.leftBtn.hidden = NO;
    self.rightBtn.hidden = NO;
    
    switch (newsOrderModel.orderStatus) {
        case 101:
            [self.leftBtn setTitle:kYQLS(@"取消订单") forState:UIControlStateNormal];
            [self.rightBtn setTitle:kYQLS(@"立即支付") forState:UIControlStateNormal];
            break;
        case 102 | 103:
            self.leftBtn.hidden = YES;
            [self.rightBtn setTitle:kYQLS(@"再次购买") forState:UIControlStateNormal];
            break;
        case 202:
            self.leftBtn.hidden = YES;
            [self.rightBtn setTitle:kYQLS(@"删除订单") forState:UIControlStateNormal];
            break;
        case 201:
            //"等待卖家发货"
            self.leftBtn.hidden = YES;
            //[self.leftBtn setTitle:@"申请售后" forState:UIControlStateNormal];
            [self.rightBtn setTitle:@"" forState:UIControlStateNormal];
            break;
        case 301:
        case 302:
        case 303:
            self.leftBtn.hidden = YES;
            [self.rightBtn setTitle:kYQLS(@"发布评价") forState:UIControlStateNormal];
            break;
        
        default:
            break;
    }

}

- (void)setOrderDetailsModel:(YQGoodsOrderDetailsModel *)orderDetailsModel {
    _orderDetailsModel = orderDetailsModel;
    self.leftBtn.hidden = NO;
    self.rightBtn.hidden = NO;
    
    if (self.isAftersale) {
        //售后订单
        switch (orderDetailsModel.aftersaleStatus) {
            case 1:
                [self.leftBtn setTitle:kYQLS(@"填写物流") forState:UIControlStateNormal];
                [self.rightBtn setTitle:kYQLS(@"撤销申请") forState:UIControlStateNormal];
                break;
            case 2:
                self.leftBtn.hidden = true;
                [self.rightBtn setTitle:kYQLS(@"撤销申请") forState:UIControlStateNormal];
                break;
            case 4:
                self.leftBtn.hidden = true;
                self.rightBtn.hidden = YES;
                //[self.rightBtn setTitle:@"撤销申请" forState:UIControlStateNormal];
                break;
            case 5:
                        
                break;
                
            default:
                break;
        }
        return;
    }
    
    switch (orderDetailsModel.orderStatus) {
        case 101:
            //"等待买家付款"
            [self.leftBtn setTitle:kYQLS(@"取消订单") forState:UIControlStateNormal];
            [self.rightBtn setTitle:kYQLS(@"立即支付") forState:UIControlStateNormal];
            break;
        case 102|103:
            self.leftBtn.hidden = YES;
            [self.rightBtn setTitle:kYQLS(@"删除订单") forState:UIControlStateNormal];
            break;
        case 202:
//            [self.leftBtn setTitle:@"取消订单" forState:UIControlStateNormal];
            self.leftBtn.hidden = YES;
            [self.rightBtn setTitle:kYQLS(@"删除订单") forState:UIControlStateNormal];
            break;
            //交易失败"
        case 201:
            //"等待卖家发货"
            self.leftBtn.hidden = YES;
            //[self.leftBtn setTitle:@"申请售后" forState:UIControlStateNormal];
            [self.rightBtn setTitle:kYQLS(@"申请售后") forState:UIControlStateNormal];
            break;
        case 301:
            //"卖家已发货";
//            self.leftBtn.hidden = YES;
            [self.leftBtn setTitle:kYQLS(@"申请售后") forState:UIControlStateNormal];
            [self.rightBtn setTitle:kYQLS(@"确认收货") forState:UIControlStateNormal];
            break;
        case 302:
        case 303:
            //"交易成功";
            self.leftBtn.hidden = orderDetailsModel.isAftersale;
            [self.leftBtn setTitle:kYQLS(@"申请售后") forState:UIControlStateNormal];
            [self.rightBtn setTitle:kYQLS(@"发布评价") forState:UIControlStateNormal];
            break;
            
        default:
            break;
    }

}

-(void)awakeFromNib {
    [super awakeFromNib];
    if (self.orderDetailsModel) {
        self.leftBtn.tag = kBTNTAG_BASE;
        self.rightBtn.tag = kBTNTAG_BASE + 1;
    }else{
        self.leftBtn.tag = YQPOrderItemLeft;
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.orderDetailsModel) {
        self.leftBtn.tag = kBTNTAG_BASE;
        self.rightBtn.tag = kBTNTAG_BASE + 1;
    }else{
        self.leftBtn.tag = YQPOrderItemLeft;
    }

    self.leftBtn.ghostColor =HexColor(0xA0A0A0);
    self.rightBtn.ghostColor = kAPPThemeColor;
    [self.leftBtn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.rightBtn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    self.rightBtn.tag = YQPOrderItemRight;

}

- (void)setModel:(YQPOrderInfoModel *)model
{
    _model = model;
    self.hidden = NO;

    if ([model.orderStatus isEqualToString:@"0"]) {
        [self.leftBtn setTitle:kYQLS(@"取消订单") forState:UIControlStateNormal];
        [self.rightBtn setTitle:kYQLS(@"立即支付") forState:UIControlStateNormal];
        self.leftBtn.hidden = NO;

    }else if ([model.orderStatus isEqualToString:@"1"]||[model.orderStatus isEqualToString:@"4"]) {
        self.hidden = YES;
    }else if ([model.orderStatus isEqualToString:@"2"]) {
        [self.rightBtn setTitle:kYQLS(@"确认收货") forState:UIControlStateNormal];
        self.leftBtn.hidden = YES;
    }else if ([model.orderStatus isEqualToString:@"3"]) {
        [self.rightBtn setTitle:kYQLS(@"删除订单") forState:UIControlStateNormal];
        self.leftBtn.hidden = YES;
    }
}

- (void)onClick:(QMUIGhostButton *)button
{
    if (self.orderDetailsModel || self.newsOrderModel) {
        if ([self.delegate respondsToSelector:@selector(selectItem:)]) {
            if (button == self.leftBtn) {
                [self.delegate selectItem:0];
            }else{
                [self.delegate selectItem:1];
            }
        }
        return;
    }
    if (button.tag == YQPOrderItemLeft) {
        if ([button.currentTitle isEqualToString:kYQLS(@"取消订单")]) {
            if ([self.delegate respondsToSelector:@selector(didClickItemToCancelModel:)]) {
                [self.delegate didClickItemToCancelModel:self.model];
            }
        }else if ([button.currentTitle isEqualToString:kYQLS(@"删除订单")]) {
            if ([self.delegate respondsToSelector:@selector(didClickItemToRemoveModel:)]) {
                [self.delegate didClickItemToRemoveModel:self.model];
            }
        }
    }else{
        if ([button.currentTitle isEqualToString:kYQLS(@"立即支付")]) {
            if ([self.delegate respondsToSelector:@selector(didClickItemToPayModel:)]) {
                [self.delegate didClickItemToPayModel:self.model];
            }
        }else if ([button.currentTitle isEqualToString:kYQLS(@"重新下单")]) {
            if ([self.delegate respondsToSelector:@selector(didClickItemToOrderModel:)]) {
                [self.delegate didClickItemToOrderModel:self.model];
            }
        }else if ([button.currentTitle isEqualToString:kYQLS(@"发布评价")]) {
            if ([self.delegate respondsToSelector:@selector(didClickItemToCommentModel:)]) {
                [self.delegate didClickItemToCommentModel:self.model];
            }
        }else if ([button.currentTitle isEqualToString:kYQLS(@"确认收货")]) {
            if ([self.delegate respondsToSelector:@selector(didClickItemToConfirmReceiveModel:)]) {
                [self.delegate didClickItemToConfirmReceiveModel:self.model];
            }
        }else if ([button.currentTitle isEqualToString:kYQLS(@"删除订单")]) {
            if ([self.delegate respondsToSelector:@selector(didClickItemToRemoveModel:)]) {
                [self.delegate didClickItemToRemoveModel:self.model];
            }
        }
    }
}
@end
